package com.spider.Demo;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        //目标：简易计算器开发
        //输入：两个数字，运算符
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个数字：");
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        System.out.println("请输入运算符：");
        String operator = sc.next();
        //把这个数据交给方法处理，帮我计算并返回给我
        double result = calculate(a,b,operator);
        // 判断是否是 NaN，如果不是才打印
        if (!Double.isNaN(result)) {
            System.out.println("计算结果为：" + result);
        }
    }
    public static double calculate(double a,double b,String operator) {
        double result = 0;

        switch (operator) {
            case "+":
                result = a + b;
                break;
            case "-":
                result = a - b;
                break;
            case "*":
                result = a * b;
                break;
            case "/":
                if (b == 0) {
                    System.out.println("除数不能为0！");
                    return Double.NaN;
                }
                result = a / b;
                break;
            default:
                System.out.println("输入的运算符有误！");
                return 0;
        }
        return result;
    }

}
